package org.jeecg.modules.appuser.job;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.Date.DateUtil;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.appuser.constant.LuckDrawConstant;
import org.jeecg.modules.appuser.constant.PayTypeConstant;
import org.jeecg.modules.appuser.entity.*;
import org.jeecg.modules.appuser.service.*;
import org.jeecg.modules.mall.service.IDtkManager;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import static org.jeecg.modules.appuser.job.LuckDrawByWeekTask.getRandom;

/**
 * <p>
 * 抽奖 每天凌晨1点结束
 * 系统创建 和关闭抽奖任务
 * </p>
 * 9/29
 */
@Slf4j
public class CreateLuckDrawByDayTask implements Job {

    @Autowired
    private ITbUserLuckdrawService tbUserLuckdrawService;

    @Autowired
    private ITbUserLuckdrawConfigService tbUserLuckdrawConfigService;

    @Autowired
    private ITbUserWalletDetailService tbUserWalletDetailService;
    /**
     * 定时任务执行入口
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("抽奖定时任务开始运行,dateTime:{}", DateUtils.now());
        //先暂停 抽奖，  抽取  用户， 如果没有用户，则作废这期，
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state", 0);
        queryWrapper.eq("luckdraw_type", 1);//每日抽奖
        queryWrapper.le("create_time", new Date());
        queryWrapper.ge("end_time", new Date());
        TbUserLuckdraw tbUserLuckdraw = tbUserLuckdrawService.getOne(queryWrapper);
        //今天没有抽奖选项，则创建今天的抽奖。
        if (null == tbUserLuckdraw) {
            QueryWrapper configQueryWrapper = new QueryWrapper();
            configQueryWrapper.eq("is_auto", 1);
            List<TbUserLuckdrawConfig> tbUserLuckdrawConfigList = tbUserLuckdrawConfigService.list(configQueryWrapper);
            if (null != tbUserLuckdrawConfigList && tbUserLuckdrawConfigList.size() > 0) {

                for (TbUserLuckdrawConfig tbUserLuckdrawConfig : tbUserLuckdrawConfigList) {
                    tbUserLuckdraw = new TbUserLuckdraw();
                    BeanUtil.copyProperties(tbUserLuckdrawConfig, tbUserLuckdraw);
                    Date startTime = tbUserLuckdrawConfig.getStartTime();
                    Date endTime = tbUserLuckdrawConfig.getEndTime();
                    int day = DateUtil.subDay(new Date(), startTime) +1;
                    Date new_startTime = DateUtil.addOneDay(startTime, day);
                    tbUserLuckdraw.setStartTime(new_startTime);
                    //加时间
                    int end_day = DateUtil.subDay(new Date(), endTime)+1 ;
                    Date new_endTime = DateUtil.addOneDay(endTime, end_day);
                    tbUserLuckdraw.setEndTime(new_endTime);
                    tbUserLuckdraw.setId(UUIDGenerator.generate());
                    Date startSystemTime = tbUserLuckdrawConfig.getStartSystemTime();
                    Date endSystemTime = tbUserLuckdrawConfig.getEndSystemTime();
                    int day1 = DateUtil.subDay(new Date(), startTime)+1 ;
                    Date new_startSystemTime = DateUtil.addOneDay(startSystemTime, day1);
                    tbUserLuckdraw.setStartSystemTime(new_startSystemTime);

                    int day2 = DateUtil.subDay(new Date(), startTime) +1;
                    Date new_endSystemTime = DateUtil.addOneDay(endSystemTime, day2);
                    tbUserLuckdraw.setEndSystemTime(new_endSystemTime);
                    tbUserLuckdraw.setState(2);
                    tbUserLuckdrawService.save(tbUserLuckdraw);
                }
            }


        }


        log.info("抽奖客定时任务开始结束,dateTime:{}", DateUtils.now());
    }


    private void saveUserWalletDetail(TbUserWallet tbUserWallet, BigDecimal newBalance) {
        TbUserWalletDetail tbUserWalletDetail = new TbUserWalletDetail();
        tbUserWalletDetail.setPayFrom(PayTypeConstant.PAYFORM_LUCKDRAW);
        tbUserWalletDetail.setPayType(PayTypeConstant.PAYTYPE_INCOME);
        tbUserWalletDetail.setPayNumber(newBalance);
        tbUserWalletDetail.setWalletId(tbUserWallet.getId());
        tbUserWalletDetail.setUserId(tbUserWallet.getUserId());
        tbUserWalletDetailService.save(tbUserWalletDetail);
    }

    public static int getRandom(int max) {

        int min = 1;
        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        return s;
    }
}